package com.xiangty.job;

import com.xiangty.bean.CustTemp;
import com.xiangty.listener.MyJobListener;
import com.xiangty.partition.CustTempToInfoPartitioner;
import com.xiangty.write.CustInfoWrite;
import com.xiangty.write.CustTempWrite;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.partition.PartitionHandler;
import org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;

@Configuration
@EnableBatchProcessing
public class ImportantJobNew {

    private static final String STEP1 = "fileToTempTableStep";
    private static final String STEP2 = "tyImportantStep1";

    // 注入创建任务对象的对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    // 任务的执行由Step决定
    // 注入穿件Step对象的对象
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("taskExecutor")
    private TaskExecutor taskExecutor;

    @Autowired
    private CustTempToInfoPartitioner custTempToInfoPartitioner;

    // 创建任务对象
    @Bean("tyImportantJob")
    public Job tyImportantJob(
            @Qualifier(STEP1) Step fileToTempTableStep,
            @Qualifier("catMasterStep") Step catMasterStep
    ) {
        System.out.println("---------------任务【tyImportantJob】开始---------------");
        // helloWordJob任务只执行step1一个步骤
        return jobBuilderFactory.get("tyImportantJob")
//                .start(catMasterStep) // t_cust_temp入t_cust_info表
                .start(fileToTempTableStep) // 先解析文件入t_cust_temp
                .next(catMasterStep)  // 再执行 t_cust_temp入t_cust_info表
                .listener(new MyJobListener())
                .build();
    }

    /**
     * 解析文件将数据存入到数据库中
     */
    @Bean(STEP1)
    public Step fileToTempTableStep(@Qualifier("flatFileItemReader") FlatFileItemReader<CustTemp> flatFileItemReader,
                                    @Qualifier("custTempWrite") CustTempWrite custTempWrite,
                                    @Qualifier("taskExecutor") TaskExecutor taskExecutor) {
        return stepBuilderFactory.get(STEP1)
                .<CustTemp, CustTemp>chunk(100)
                .reader(flatFileItemReader)
                .writer(custTempWrite)
                .taskExecutor(taskExecutor)
                .throttleLimit(8)
                .build();
    }

    @Bean("catMasterStep")
    public Step catMasterStep(@Qualifier("partitionHandler") PartitionHandler partitionHandler) {
        return stepBuilderFactory.get("catMasterStep").partitioner(STEP2, custTempToInfoPartitioner)
                .partitionHandler(partitionHandler).build();
    }

    @Bean("partitionHandler")
    public PartitionHandler partitionHandler(@Qualifier(STEP2) Step tyImportantStep1) {
        TaskExecutorPartitionHandler handler = new TaskExecutorPartitionHandler();
        handler.setGridSize(4);
        handler.setTaskExecutor(taskExecutor);
        handler.setStep(tyImportantStep1);
        try {
            handler.afterPropertiesSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return handler;
    }

    /**
     * MyBatisPagingItemReader方式分片：@Qualifier("myBatisPagingItemReader") MyBatisPagingItemReader<CustTemp> custTempItemReader,
     * MyBatisCursorItemReader方式分片：@Qualifier("custTempMyBatisCursorItemReader") MyBatisCursorItemReader<CustTemp> custTempItemReader,
     * JdbcPagingItemReader方式分片: @Qualifier("custTempItemReader2") JdbcPagingItemReader<CustTemp> custTempItemReader,
     * JdbcCursorItemReader方式分片: @Qualifier("jdbcCursorItemReader") JdbcCursorItemReader<CustTemp> custTempItemReader,
     */
    @Bean(STEP2)
    public Step tyImportantStep1(
//            @Qualifier("myBatisPagingItemReader") MyBatisPagingItemReader<CustTemp> custTempItemReader,
//            @Qualifier("custTempMyBatisCursorItemReader") MyBatisCursorItemReader<CustTemp> custTempItemReader,
            @Qualifier("custTempItemReader2") JdbcPagingItemReader<CustTemp> custTempItemReader,
//            @Qualifier("jdbcCursorItemReader") JdbcCursorItemReader<CustTemp> custTempItemReader,
            @Qualifier("custInfoWrite") CustInfoWrite custInfoWrite
    ) {
        return stepBuilderFactory.get(STEP2)
                .<CustTemp, CustTemp>chunk(1000)
                .reader(custTempItemReader)
                .writer(custInfoWrite)
//                .taskExecutor(new SimpleAsyncTaskExecutor())
//                .taskExecutor(taskExecutor)
//                .throttleLimit(4)
                .build();
    }

}
